package com.net.ip;

import java.io.IOException;
import java.net.InetAddress;
import jpcap.*;
import jpcap.packet.*;



@SuppressWarnings("unused")
public class IpScan {
	

	
	
	private IPPacket ip_packet = null;
	private NetworkInterface[] devices;
	private int cap_index;
	private JpcapCaptor captor;

	public void demo(int index) throws IOException
	{
		this.openDevies();
		this.setCapIndex(index);
		this.setFilter();
		this.ipPacketAnalysis();
	}
	
	
	public void ipPacketAnalysis() {
		Packet packet;
		while (true) {
			ip_packet = (IPPacket)captor.getPacket();
			if (ip_packet != null) {
			System.out.println("src_ip :" + ip_packet.src_ip);
			System.out.println("dst_ip :" + ip_packet.dst_ip);
			System.out.println("length :" + ip_packet.length);
			}
		}

	}
	
	

	public void openDevies() throws IOException {
		this.captor = JpcapCaptor.openDevice(devices[cap_index], 65535, false, 20);
	}

	public void getDevics() {
		this.devices = JpcapCaptor.getDeviceList();
	}

	public void setFilter() throws IOException {
		this.captor.setFilter("ip and tcp", true);
	}

	public Packet getPacket() {
		return this.captor.getPacket();
	}

	public void setCapIndex(int index) {
		this.cap_index = index;
	}

}
